package com.zs.lib_base.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.zs.lib_base.bean.InterestSaveBean
import com.zs.lib_base.ext.toJson

/**
 * sp
 */
open class SPHelpers(context: Context) {
    val prefs: SharedPreferences by lazy {
        context.getSharedPreferences(
            "zs_sp",
            Context.MODE_PRIVATE
        )
    }

    fun getToken(): String {
        return getSharedPreferences("token", "")
    }

    fun setToken(value: String) {
        putSharedPreferences("token", value)
    }

    fun getInterest(): String {
        return getSharedPreferences("interest", "")
    }

    fun setInterest(value: String) {
        putSharedPreferences("interest", value)
    }

    fun setInterestList(value: MutableList<InterestSaveBean>) {
        putSharedPreferences("interestList", value.toJson())
    }


    fun getInterestList(): String {
        return getSharedPreferences("interestList", "")
    }

    fun isChoseInterest():Boolean{
        return getInterest() != ""
    }
    fun getSex(): String {
        return getSharedPreferences("token", "")
    }

    fun setSex(value: String) {
        putSharedPreferences("sex", value)
    }

    fun isGuide(): Boolean {
       return getGuide().isEmpty()
    }

    fun setGuide(value: String) {
        putSharedPreferences("isGuide", value)
    }
    fun getGuide(): String  {
        return getSharedPreferences("isGuide", "")
    }

    fun setVersion(value: String) {
        putSharedPreferences("version", value)
    }
    fun getVersion(): String  {
        return getSharedPreferences("version", "")
    }


    @SuppressLint("CommitPrefEdits")
    private fun putSharedPreferences(name: String, value: String) = with(prefs.edit()) {
        putString(name, value).apply()
    }

    @Suppress("UNCHECKED_CAST")
    private fun getSharedPreferences(name: String, default: String): String = with(prefs) {
        return getString(name, default)!!
    }

    fun clearSp() = with(prefs.edit()) {
        clear()
        commit()
    }
    fun logout()= with(prefs.edit()){

    }
}